<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- threads.qdoc -->
  <title>Thread-Support in Qt Modules | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Thread-Support in Qt Modules</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="threads-qobject.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="threads-qobject.html">Threads and QObjects</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#threads-and-the-sql-module">Threads and the SQL Module</a></li>
<li class="level1"><a href="#painting-in-threads">Painting in Threads</a></li>
<li class="level1"><a href="#threads-and-rich-text-processing">Threads and Rich Text Processing</a></li>
<li class="level1"><a href="#threads-and-the-svg-module">Threads and the SVG Module</a></li>
<li class="level1"><a href="#threads-and-implicitly-shared-classes">Threads and Implicitly Shared Classes</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Thread-Support in Qt Modules</h1>
<span class="subtitle"></span>
<!-- $$$threads-modules.html-description -->
<div class="descr"> <a name="details"></a>
<a name="threads-and-the-sql-module"></a>
<h2 id="threads-and-the-sql-module">Threads and the SQL Module</h2>
<p>A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.</p>
<p>In addition, the third party libraries used by the QSqlDrivers can impose further restrictions on using the SQL Module in a multithreaded program. Consult the manual of your database client for more information</p>
<a name="painting-in-threads"></a>
<h2 id="painting-in-threads">Painting in Threads</h2>
<p><a href="../qtgui/qpainter.html">QPainter</a> can be used in a thread to paint onto <a href="../qtgui/qimage.html">QImage</a>, <a href="../qtprintsupport/qprinter.html">QPrinter</a>, and <a href="../qtgui/qpicture.html">QPicture</a> paint devices. Painting onto QPixmaps and QWidgets is <i>not</i> supported. On macOS the automatic progress dialog will not be displayed if you are printing from outside the GUI thread.</p>
<p>Any number of threads can paint at any given time, however only one thread at a time can paint on a given paint device. In other words, two threads can paint at the same time if each paints onto separate QImages, but the two threads cannot paint onto the same <a href="../qtgui/qimage.html">QImage</a> at the same time.</p>
<a name="threads-and-rich-text-processing"></a>
<h2 id="threads-and-rich-text-processing">Threads and Rich Text Processing</h2>
<p>The <a href="../qtgui/qtextdocument.html">QTextDocument</a>, <a href="../qtgui/qtextcursor.html">QTextCursor</a>, and <a href="../qtgui/richtext.html">all related classes</a> are reentrant.</p>
<p>Note that a <a href="../qtgui/qtextdocument.html">QTextDocument</a> instance created in the GUI thread may contain <a href="../qtgui/qpixmap.html">QPixmap</a> image resources. Use <a href="../qtgui/qtextdocument.html#clone">QTextDocument::clone</a>() to create a copy of the document, and pass the copy to another thread for further processing (such as printing).</p>
<a name="threads-and-the-svg-module"></a>
<h2 id="threads-and-the-svg-module">Threads and the SVG Module</h2>
<p>The <a href="../qtsvg/qsvggenerator.html">QSvgGenerator</a> and <a href="../qtsvg/qsvgrenderer.html">QSvgRenderer</a> classes in the <a href="../qtsvg/qtsvg-module.html">QtSvg</a> module are reentrant.</p>
<a name="threads-and-implicitly-shared-classes"></a>
<h2 id="threads-and-implicitly-shared-classes">Threads and Implicitly Shared Classes</h2>
<p>Qt uses an optimization called <a href="../qtcore/implicit-sharing.html">implicit sharing</a> for many of its value class, notably <a href="../qtgui/qimage.html">QImage</a> and <a href="../qtcore/qstring.html">QString</a>. Beginning with Qt 4, implicit shared classes can safely be copied across threads, like any other value classes. They are fully <a href="threads-reentrancy.html">reentrant</a>. The implicit sharing is really <i>implicit</i>.</p>
<p>In many people's minds, implicit sharing and multithreading are incompatible concepts, because of the way the reference counting is typically done. Qt, however, uses atomic reference counting to ensure the integrity of the shared data, avoiding potential corruption of the reference counter.</p>
<p>Note that atomic reference counting does not guarantee <a href="threads-reentrancy.html">thread-safety</a>. Proper locking should be used when sharing an instance of an implicitly shared class between threads. This is the same requirement placed on all <a href="threads-reentrancy.html">reentrant</a> classes, shared or not. Atomic reference counting does, however, guarantee that a thread working on its own, local instance of an implicitly shared class is safe. We recommend using <a href="threads-qobject.html#signals-and-slots-across-threads">signals and slots</a> to pass data between threads, as this can be done without the need for any explicit locking.</p>
<p>To sum it up, implicitly shared classes in Qt 4 are really <i>implicitly</i> shared. Even in multithreaded applications, you can safely use them as if they were plain, non-shared, reentrant value-based classes.</p>
</div>
<!-- @@@threads-modules.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="threads-qobject.html">Threads and QObjects</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
